package ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.List;
import bean.Files;
import bean.Folders;
import service.IFileService;
import service.impl.IFileServiceImpl;

public class FilePanel extends JPanel {
    private JList<Files> fileList; // 文件列表组件
    private DefaultListModel<Files> listModel; // 列表数据模型
    private JScrollPane scrollPane; // 滚动面板
    private String currentFolderId; // 当前文件夹ID
    
    private Folders folder;
    private IFileService ifileservice = new IFileServiceImpl();

    public FilePanel(Folders folder) {
        this.folder = folder;
        initUI();
        loadData(folder.getFolderName(), folder.getFolderId()); // 加载数据时传入正确的参数
        initListener();
    }

	public Folders getFolder() {
        return this.folder;
    }
    public JList<Files> getFileList(){
    	return fileList;
    }

    /**
     * 初始化界面布局
     */
    private void initUI() {
        setLayout(new BorderLayout());
        setBorder(new EmptyBorder(5, 5, 5, 5)); // 设置边距

        // 初始化列表模型和组件
        listModel = new DefaultListModel<>();
        fileList = new JList<>(listModel);
        
        // 自定义单元格渲染器(显示文件名和图标)
        fileList.setCellRenderer(new FileListCellRenderer());
        fileList.setFixedCellHeight(30); // 设置行高以适应图标

        // 设置滚动面板并隐藏滚动条
        scrollPane = new JScrollPane(fileList);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        add(scrollPane, BorderLayout.CENTER);
    }
    //事件监听
    private void initListener() {
    	fileList.addMouseListener(new MouseAdapter() {
    	    @Override
    	    public void mouseClicked(MouseEvent e) {
    	        if (SwingUtilities.isRightMouseButton(e)) {
    	            Files selectedFile = fileList.getSelectedValue();
    	            if (selectedFile != null) {
    	                ContextMenu.showJavaContextMenu(fileList,selectedFile.getFilePath());
    	                loadData(folder.getFolderName(), folder.getFolderId());//有删改操作,刷新一遍列表
    	            }
    	        }else if (e.getClickCount() == 2) {
                    // 双击打开文件
                    Files selectedFile = fileList.getSelectedValue();
                    if (selectedFile != null) {
                        openFileDirectly(selectedFile.getFilePath());
                    }
                }
    	    }
    	});
    	// 添加鼠标移动监听器以实现悬停选择
        fileList.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                // 获取鼠标当前位置对应的列表项索引
                int index = fileList.locationToIndex(e.getPoint());
                if (index != -1) {
                    // 检查鼠标是否确实在该项的边界内
                    Rectangle cellBounds = fileList.getCellBounds(index, index);
                    if (cellBounds != null && cellBounds.contains(e.getPoint())) {
                        fileList.setSelectedIndex(index);
                    } else {
                        fileList.clearSelection();
                    }
                } else {
                    fileList.clearSelection();
                }
            }
            
            @Override
            public void mouseExited(MouseEvent e) {
                // 鼠标离开列表区域时清除选择
                fileList.clearSelection();
            }
        });
    }
    
    /**
     * 加载文件列表数据
     * @param folderName 文件夹名称
     * @param folderId 文件夹ID
     */
    public void loadData(String folderName, String folderId) {
        this.currentFolderId = folderId;
        listModel.clear(); // 清空旧数据

        // 调用接口获取数据
        Folders folderData = ifileservice.getFileListById(folderName, folderId);
        if (folderData != null && folderData.getFilelist() != null) {
            // 添加文件到列表
            for (Files file : folderData.getFilelist()) {
                if (file.getFilePath() != null && !file.getFilePath().isEmpty()) {
                    listModel.addElement(file);
                }
            }
        } else {
            listModel.addElement(new Files("暂无文件", "", null, folderId)); // 空状态提示
        }
    }
    /**
     * 打开文件
     * @param filePath 文件路径
     */
    private void openFileDirectly(String filePath) {
        try {
            // 使用Desktop类打开文件
            Desktop.getDesktop().open(new File(filePath));
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, 
                    "无法打开文件: " + ex.getMessage(), 
                    "错误", 
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * 获取当前文件夹ID
     */
    public String getCurrentFolderId() {
        return currentFolderId;
    }
    
}